package com.hongshan.wallet.core.wallet

import android.app.Application
import android.databinding.ObservableField
import android.view.Gravity
import android.view.View
import com.hongshan.wallet.Constant
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.makeSalt
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import com.hongshan.wallet.util.AutoInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.wallet
 * @author   : zhangxx
 * @date     : 2019/3/26
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
class RealNameViewModel(application: Application) :MyViewModel(application) {

    val name = ObservableField<String>()
    val idCard = ObservableField<String>()
    val gravity = ObservableField(Gravity.LEFT)
    val canRealName = ObservableField(false)

    @AutoInject(Constant.ROUTE_PARAMS_KEY.REAL_NAME_ACTION)
    val action: String? = null
    @AutoInject(Constant.ROUTE_PARAMS_KEY.PARAM_ACCOUNT_TYPE)
    val accountType: String? = null
    @AutoInject(Constant.ROUTE_PARAMS_KEY.REAL_NAME_NEED_FINISH)
    val needFinish:Boolean = false


    val commit = View.OnClickListener {
        val nameStr = name.get()?.trim()
        val idCardStr = idCard.get()?.trim()
        if (nameStr == null || !"\\w{1,8}".toRegex().matches(nameStr)) {
            toast("姓名格式不正确")
            return@OnClickListener
        }
        if (idCardStr == null || !"\\d{17}[Xx\\w]".toRegex().matches(idCardStr)) {
            toast("身份证格式不正确")
            return@OnClickListener
        }
        when (action) {
            Constant.ROUTE_PARAMS_KEY.ACTION_CHECK_REAL_NAME -> {
                checkRealName(nameStr, idCardStr)
            }
            Constant.ROUTE_PARAMS_KEY.ACTION_SET_REAL_NAME -> {
                realName(nameStr, idCardStr)
            }
        }
    }

    private fun checkRealName(nameStr: String, idCardStr: String) {
        GlobalScope.launch(Dispatchers.Main) {
            showLoading.call()
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getRealName().execute()
                }
            }
            val body = result.await()?.body()
            showSuccess.call()
            if (body?.code == 0) {
                if (body.data.real_name_status == 1) {
                    val real_name = body.data.real_name_data.real_name
                    val credential_no = body.data.real_name_data.credential_no
                    if (nameStr == real_name && idCardStr == credential_no) {
                        val map = mapOf(
                            Constant.ROUTE_PARAMS_KEY.SMS_TITLE to "手机验证",
                            Constant.ROUTE_PARAMS_KEY.SMS_BUTTON to "确定解除绑定",
                            Constant.ROUTE_PARAMS_KEY.SMS_ACTION to Constant.ROUTE_PARAMS_KEY.ACTION_UNBIND_WX_OR_ALIPAY,
                            Constant.ROUTE_PARAMS_KEY.PARAM_ID_CARD to credential_no,
                            Constant.ROUTE_PARAMS_KEY.PARAM_REAL_NAME to real_name,
                            Constant.ROUTE_PARAMS_KEY.PARAM_ACCOUNT_TYPE to accountType,
                            Constant.ROUTE_PARAMS_KEY.SMS_NEED_FINISH to true
                        )
                        go(RouterIndex.SEND_SMS,map)
                    } else {
                        toast("姓名或身份证号不正确")
                    }
                } else{
                    toast("当前未实名认证")
                }
            }
        }


    }

    private fun realName(nameStr: String, idCardStr: String) {
        go(RouterIndex.ADD_CARD, mapOf(Constant.ROUTE_PARAMS_KEY.REAL_NAME_NAME to nameStr,Constant.ROUTE_PARAMS_KEY.REAL_NAME_ID_CARD to idCardStr))
    }

    fun getRealNameInfo() {
        GlobalScope.launch(Dispatchers.Main) {
            showLoading.call()
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getRealName().execute()
                }
            }
            val body = result.await()?.body()
            showSuccess.call()
            if (body?.code == 0) {
                if (body.data.real_name_status == 1) {
                    val real_name = body.data.real_name_data.real_name
                    val credential_no = body.data.real_name_data.credential_no
                    name.set(real_name.makeSalt())
                    idCard.set(credential_no.makeSalt())
                }
            }
        }
    }

    override fun initial() {
        super.initial()
        when (action) {
            Constant.ROUTE_PARAMS_KEY.ACTION_CHECK_REAL_NAME -> {
                canRealName.set(true)
            }
            Constant.ROUTE_PARAMS_KEY.ACTION_SET_REAL_NAME -> {
                canRealName.set(true)
            }
            Constant.ROUTE_PARAMS_KEY.ACTION_LOOK_REAL_NAME -> {
                gravity.set(Gravity.RIGHT)
                getRealNameInfo()
            }
        }
    }

}